﻿using DtoFw;
using DtoFw.Enum;
using Hangfire;
using Repository;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Util;
using System.Linq;
using Microsoft.AspNetCore.Hosting;

namespace Service
{
    public class TaskService
    {
        //隔多久执行
        //RecurringJob.AddOrUpdate("1", () => A(), "* 52 15 12 4 *");   //每年的4月12号15点52分任意秒执行 没加时区 小时+8
        //RecurringJob.AddOrUpdate("2", () => A(), Cron.Minutely);      //每分钟执行一次。
        //RecurringJob.AddOrUpdate("3", () => A(), Cron.Daily(18, 36), TimeZoneInfo.Local);  //每天18点36分 当前时区
        //RecurringJob.AddOrUpdate<ITaskInterface>(x => x.Test(), Cron.Daily(18, 25),TimeZoneInfo.Local);
        private readonly IRecurringJobManager _recurringJobManager;
        private readonly MainDicService _dicService;
        private readonly EarlyWarningService _earlyWarningService;
        private readonly MainHomeService _mainConfigService;
        private readonly DbBakService _dbBakService;

        public TaskService()
        {
            this._recurringJobManager = EngineContext.Resolve<IRecurringJobManager>();
            this._dicService = EngineContext.Resolve<MainDicService>();
            this._earlyWarningService = EngineContext.Resolve<EarlyWarningService>();
            this._mainConfigService = EngineContext.Resolve<MainHomeService>();
            this._dbBakService = EngineContext.Resolve<DbBakService>();
        }

        /// <summary>
        /// 启动服务
        /// </summary>
        /// <returns></returns>
        [Obsolete]
        public BaseResult PulseOnHangfireServer()
        {
            this._recurringJobManager.AddOrUpdate("EarlyWarnEvidenceUnbound",
                () => EarlyWarnEvidenceUnbound(), Cron.MinuteInterval(5));

            return BaseResult.Succeed();
        }

        #region 预警接口

        /// <summary>
        /// 物品未绑定
        /// </summary>
        /// <returns></returns>
        public bool EarlyWarnEvidenceUnbound()
        {
            return _earlyWarningService.EarlyWarnEvidenceUnbound();
        }

        /// <summary>
        /// 未及时入库
        /// </summary>
        /// <returns>是否成功</returns>
        public bool EarlyWarnOvertimeStock()
        {
            return _earlyWarningService.EarlyWarnOvertimeStock();
        }

        /// <summary>
        /// 未及时回库
        /// </summary>
        /// <returns>是否成功</returns>
        public bool EarlyWarnOvertimeRevert()
        {
            return _earlyWarningService.EarlyWarnOvertimeRevert();
        }

        /// <summary>
        /// 保存超期
        /// </summary>
        /// <returns>是否成功</returns>
        public bool EarlyWarnOvertimeSave()
        {
            return _earlyWarningService.EarlyWarnOvertimeSave();
        }

        /// <summary>
        /// 库容爆仓
        /// </summary>
        /// <returns>是否成功</returns>
        public bool EarlyWarnStockQuantity()
        {
            return _earlyWarningService.EarlyWarnStockQuantity();
        }
        
        public bool DbBakTask()
        {
            return _dbBakService.DbBakTask();
        }

        public bool SendTestMsg()
        {
            var sw = new System.IO.StreamWriter("d:\\a.txt",true);
            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sw.Close();
            try
            {
                _earlyWarningService.SendTestMsg();
            }
            catch (Exception)
            {

            }
            return true;
        }

        public bool PingWebSocket()
        {
            WebSocketModel.SendJson("ping", new List<string>());
            return true;
        }

        public int GetInterval(string warnType)
        {
            return 0;
        }

        #endregion
    }
}
